package day11.problem1;

import java.util.Scanner;

//买卖股票的最佳时机2
//https://www.nowcoder.com/practice/fbc5dad3e215457fb82a3ae688eb7281?tpId=230&tqId=39768&ru=/exam/oj

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int ret = 0;
        int n = in.nextInt();
        int[] prices = new int[n];
        for(int i = 0; i<n; i++) {
            prices[i] = in.nextInt();
        }
        for(int i = 1; i<n; i++) {
            if(prices[i] > prices[i-1]) ret += (prices[i]-prices[i-1]);
        }
        System.out.println(ret);
    }
}